﻿#ifdef WIN32
#pragma execution_character_set("utf-8")
#endif

#include "DAddHomeworkView.h"

#include <QHBoxLayout>
#include <QVBoxLayout>

#include "qheaderview.h"

constexpr int ADD_COLUMN = 4;

DAddHomeworkView::DAddHomeworkView(QWidget* parent)
    : QDialog(parent)
{
    setWindowTitle("特定作业管理");

    m_table = new QTableWidget();
    m_table->setColumnCount(ADD_COLUMN);
    m_table->horizontalHeader()->hide();
    m_table->verticalHeader()->hide();
    m_table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    m_dtCurrent = new QDateEdit();
    m_dtCurrent->setFixedWidth(100);
    //    m_dtCurrent->setEnabled(false);

    m_pCombBox = new QComboBox();
    m_pCombBox->setFixedWidth(140);
    m_pCombBox->setEditable(false);
    connect(m_pCombBox,
            (void (QComboBox::*)(int))(&QComboBox::currentIndexChanged),
            [this](int index)
            {
                emit signalChangeHomework(m_dtCurrent->date(),
                                          m_pCombBox->itemData(index).toInt());
            });

    QHBoxLayout* layoutAction = new QHBoxLayout();

    QPushButton* btnAdd = new QPushButton("确认录入");
    connect(btnAdd, &QPushButton::clicked, this, &DAddHomeworkView::slotSubmit);

    QPushButton* btnFresh = new QPushButton("刷新");
    connect(btnFresh, &QPushButton::clicked,
            [this]
            {
                emit signalChangeHomework(
                    m_dtCurrent->date(),
                    m_pCombBox->itemData(m_pCombBox->currentIndex()).toInt());
            });

    m_pCheckAll = new QCheckBox("全选");
    connect(m_pCheckAll, &QCheckBox::clicked, this,
            &DAddHomeworkView::slotSelectAll);

    layoutAction->addStretch();
    layoutAction->addWidget(m_dtCurrent);
    layoutAction->addSpacing(100);
    layoutAction->addWidget(m_pCombBox);
    layoutAction->addSpacing(100);
    layoutAction->addWidget(m_pCheckAll);
    layoutAction->addSpacing(50);
    layoutAction->addWidget(btnFresh);
    layoutAction->addSpacing(50);
    layoutAction->addWidget(btnAdd);
    layoutAction->addStretch();

    QVBoxLayout* layoutMain = new QVBoxLayout();
    layoutMain->addLayout(layoutAction);
    layoutMain->addWidget(m_table);

    setLayout(layoutMain);

    resize(800, 600);
}

void DAddHomeworkView::initUI(const std::vector<DAddHomework>& vecAdd,
                              const QDate& dt,
                              const std::vector<DHomeworkType>& vecType,
                              IDHomeworkType type)
{
    QSignalBlocker blocker(m_pCombBox);
    resetTable();

    m_dtCurrent->setDate(dt);

    m_pCombBox->clear();
    for (const auto& t : vecType)
    {
        m_pCombBox->addItem(QString::fromStdString(t.strName), t.nID);
        if (type == t.nID)
        {
            m_pCombBox->setCurrentIndex(m_pCombBox->count() - 1);
        }
    }

    updateUI(vecAdd);
}

void DAddHomeworkView::updateUI(const std::vector<DAddHomework>& vecAdd)
{
    resetTable();

    const int nRowCount = std::ceil(vecAdd.size() * 1.0 / ADD_COLUMN);
    m_table->setRowCount(nRowCount);

    auto it = vecAdd.begin();
    while (it != vecAdd.end())
    {
        const int nIndex = std::distance(vecAdd.begin(), it);

        QCheckBox* check =
            new QCheckBox(QString::fromStdString((*it).student.toDesc()));

        check->setProperty("studentID", (*it).student.nID);
        check->setChecked((*it).bYes);

        const int i = nIndex / ADD_COLUMN;
        const int j = nIndex % ADD_COLUMN;
        m_table->setCellWidget(i, j, check);

        // qInfo() << i << j << nRowCount;

        ++it;
    }
}

void DAddHomeworkView::slotSubmit()
{
    const QDate dt = m_dtCurrent->date();
    const IDHomeworkType type =
        m_pCombBox->itemData(m_pCombBox->currentIndex()).toInt();

    std::vector<DAddHomework> vecSubmit;
    for (int i = 0; i < m_table->rowCount(); ++i)
    {
        for (int j = 0; j < m_table->columnCount(); ++j)
        {
            QCheckBox* check = (QCheckBox*)m_table->cellWidget(i, j);
            if (check != nullptr)
            {
                DAddHomework ah;
                ah.bYes = check->isChecked();
                ah.student.nID = check->property("studentID").toInt();
                vecSubmit.push_back(ah);
            }
        }
    }

    emit signalSubmit(dt, type, vecSubmit);
}

void DAddHomeworkView::slotSelectAll()
{
    for (int i = 0; i < m_table->rowCount(); ++i)
    {
        for (int j = 0; j < m_table->columnCount(); ++j)
        {
            QCheckBox* check = (QCheckBox*)m_table->cellWidget(i, j);
            if (check != nullptr)
            {
                check->setChecked(m_pCheckAll->isChecked() ? Qt::Checked
                                                           : Qt::Unchecked);
            }
        }
    }
}

void DAddHomeworkView::resetTable()
{
    for (int i = 0; i < m_table->rowCount(); ++i)
    {
        for (int j = 0; j < m_table->columnCount(); ++j)
        {
            QCheckBox* check = (QCheckBox*)m_table->cellWidget(i, j);
            if (check != nullptr)
            {
                m_table->setCellWidget(i, j, nullptr);

                check->setParent(nullptr);
                delete check;
            }
        }
    }
}
